namespace iScadaFBD
{
    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    [Serializable]
    public class InNodeObject
    {
        private double _LastError = 0.0;
        private double _PrevError = 0.0;
        private double _SumError = 0.0;
        private string currentValue;
        [NonSerialized]
        private System.Windows.Forms.Cursor cursor;
        private Rectangle fbRect;
        private GlobalParam.IO_Type ioType;
        private bool linked;
        private string nodeName;
        private int nodeNo;
        private string owner;
        private int upStream = 0;
        private iScadaFBD.GlobalParam.ValueType valueType;

        public InNodeObject(Rectangle fb_rect, int node_No, string node_Name)
        {
            this.IOType = GlobalParam.IO_Type.INPUT;
            this.NodeNo = node_No;
            this.NodeName = node_Name;
            this.Linked = false;
            this.fbRect = fb_rect;
            this.currentValue = "0";
            this.Owner = "";
        }

        public void Draw(Graphics g, DrawArea drawArea)
        {
            Pen pen = new Pen(Color.White, 1f);
            SolidBrush brush = new SolidBrush(Color.FromArgb(0xd0, 0xd0, 0xd0));
            Font font = new Font("Bold", 6f);
            SolidBrush brush2 = new SolidBrush(Color.FromArgb(0, 0, 0));
            g.DrawRectangle(pen, this.NodeRect());
            g.FillRectangle(brush, this.NodeRect());
            Point point = new Point(this.GetPoint().X + 8, this.GetPoint().Y);
            Point point2 = new Point(this.GetPoint().X + 8, this.GetPoint().Y + 8);
            Point point3 = new Point(this.GetPoint().X, this.GetPoint().Y + 8);
            if (this.Linked)
            {
                pen.Color = Color.FromArgb(50, 50, 50);
                g.DrawLine(pen, this.GetPoint(), point);
                g.DrawLine(pen, this.GetPoint(), point3);
                pen.Color = Color.White;
                g.DrawLine(pen, point, point2);
                g.DrawLine(pen, point2, point3);
                this.DrawTriangle(g);
            }
            else
            {
                pen.Color = Color.White;
                g.DrawLine(pen, this.GetPoint(), point);
                g.DrawLine(pen, this.GetPoint(), point3);
                pen.Color = Color.FromArgb(50, 50, 50);
                g.DrawLine(pen, point, point2);
                g.DrawLine(pen, point2, point3);
            }
            g.DrawString(this.NodeName, font, brush2, this.FontRect(font.Height, 8));
            if ((drawArea.RunMode == GlobalParam.RunMode.DEBUG) || (drawArea.RunMode == GlobalParam.RunMode.ONLINE))
            {
                this.DrawValue(g);
            }
            brush.Dispose();
            pen.Dispose();
            brush2.Dispose();
            font.Dispose();
        }

        public void DrawTriangle(Graphics g)
        {
            Point point = new Point(this.LinkPoint().X - 4, this.LinkPoint().Y - 2);
            Point point2 = new Point(this.LinkPoint().X - 4, this.LinkPoint().Y + 2);
            Pen pen = new Pen(Color.Black, 1f);
            g.DrawLine(pen, point, point2);
            g.DrawLine(pen, point, new Point(this.LinkPoint().X - 1, this.LinkPoint().Y));
            g.DrawLine(pen, point2, new Point(this.LinkPoint().X - 1, this.LinkPoint().Y));
            pen.Dispose();
        }

        public void DrawValue(Graphics g)
        {
            int num = 8;
            if (this.currentValue.Length > 0)
            {
                Font font = new Font("Bold", (float) num);
                SolidBrush brush = new SolidBrush(Color.Blue);
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Far;
                try
                {
                    Rectangle layoutRectangle = new Rectangle((this.FBRect.Left - (num * this.currentValue.Length)) - 5, this.GetPoint().Y - 8, num * this.currentValue.Length, num + 8);
                    g.DrawString(this.currentValue, font, brush, layoutRectangle, format);
                }
                catch
                {
                }
                font.Dispose();
                brush.Dispose();
                format.Dispose();
            }
        }

        private Rectangle FontRect(int fontHeight, int CharNo)
        {
            return new Rectangle((this.GetPoint().X + 8) + 1, this.GetPoint().Y, fontHeight * CharNo, fontHeight);
        }

        public System.Windows.Forms.Cursor GetCursor(Point point)
        {
            if (this.PointInNode(point))
            {
                return Cursors.Cross;
            }
            return Cursors.Default;
        }

        private Point GetPoint()
        {
            Point point = new Point(0, 0);
            point.X = this.FBRect.X + 5;
            point.Y = ((this.FBRect.Y + 15) + 5) + (0x10 * (this.NodeNo - 1));
            return point;
        }

        public Point LinkPoint()
        {
            Point point = new Point(0, 0);
            point.X = this.FBRect.X;
            point.Y = this.GetPoint().Y + 4;
            return point;
        }

        public Rectangle NodeRect()
        {
            return new Rectangle(this.GetPoint().X, this.GetPoint().Y, 8, 8);
        }

        public bool PointInNode(Point point)
        {
            return this.NodeRect().Contains(point);
        }

        [Browsable(false)]
        public string CurrentValue
        {
            get
            {
                return this.currentValue;
            }
            set
            {
                this.currentValue = value;
            }
        }

        [Browsable(false)]
        public System.Windows.Forms.Cursor Cursor
        {
            get
            {
                return this.cursor;
            }
            set
            {
                this.cursor = value;
            }
        }

        [Browsable(true), Category("位置尺寸"), Description("对象的位置与尺寸")]
        public Rectangle FBRect
        {
            get
            {
                return this.fbRect;
            }
            set
            {
                this.fbRect = value;
            }
        }

        [Browsable(false)]
        public GlobalParam.IO_Type IOType
        {
            get
            {
                return this.ioType;
            }
            set
            {
                this.ioType = value;
            }
        }

        [Browsable(false)]
        public double LastError
        {
            get
            {
                return this._LastError;
            }
            set
            {
                this._LastError = value;
            }
        }

        [Browsable(false)]
        public bool Linked
        {
            get
            {
                return this.linked;
            }
            set
            {
                this.linked = value;
            }
        }

        [Browsable(false)]
        public string NodeName
        {
            get
            {
                return this.nodeName;
            }
            set
            {
                this.nodeName = value;
            }
        }

        [Browsable(false)]
        public int NodeNo
        {
            get
            {
                return this.nodeNo;
            }
            set
            {
                this.nodeNo = value;
            }
        }

        [Description("对象关联的变量：为数字时认为是直接输入的值，为字符串时认为是物理变量"), Category("关联变量"), Browsable(true)]
        public string Owner
        {
            get
            {
                return this.owner;
            }
            set
            {
                this.owner = value;
            }
        }

        [Browsable(false)]
        public double PrevError
        {
            get
            {
                return this._PrevError;
            }
            set
            {
                this._PrevError = value;
            }
        }

        [Browsable(false)]
        public double SumError
        {
            get
            {
                return this._SumError;
            }
            set
            {
                this._SumError = value;
            }
        }

        [Browsable(false)]
        public int UpStream
        {
            get
            {
                return this.upStream;
            }
            set
            {
                this.upStream = value;
            }
        }

        [Browsable(false)]
        public iScadaFBD.GlobalParam.ValueType ValueType
        {
            get
            {
                return this.valueType;
            }
            set
            {
                this.valueType = value;
            }
        }
    }
}

